package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.PracticePlan;
import com.xmy.cultivate.entity.PracticeSubjectsType;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.StudentGrade;
import com.xmy.cultivate.entity.response.PracticePlanStaff;
import com.xmy.cultivate.service.IPracticeSubjectsTypeService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 科目类型 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/api/practiceSubjectsType")
@Scope("prototype")
public class PracticeSubjectsTypeController extends BaseController{

    @Autowired
    @Lazy
    IPracticeSubjectsTypeService iPracticeSubjectsTypeService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @GetMapping("")
    @ApiOperation("获取完成率的科目类型")
    public Result getPracticeSubjectsTypeList(@RequestParam Map reMap) {
        Quarter quarter = iQuarterService.getNowQuarter();

        Integer yearPart = StringUtils.isEmpty((String) reMap.get("yearPart")) ? quarter.getYearPart() : Integer.valueOf((String) reMap.get("yearPart"));
        Integer quarterNum = StringUtils.isEmpty((String) reMap.get("quarterNum")) ? quarter.getNum() : Integer.valueOf((String) reMap.get("quarterNum"));
        Integer subjectsId = StringUtils.isEmpty((String) reMap.get("subjectsId")) ? 1 : Integer.valueOf((String) reMap.get("subjectsId"));

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("t1.subjects_id", subjectsId);
        wrapper.eq("t1.year_part", yearPart);
        wrapper.eq("t1.quarter_num", quarterNum);
        wrapper.orderByAsc("t1.sort");
        List<PracticeSubjectsType> practiceSubjectsTypeList = iPracticeSubjectsTypeService.getPracticeSubjectsTypeList(wrapper);

        return new Result(Code.OK, practiceSubjectsTypeList, "获取完成率的科目类型");
    }

    @GetMapping("/practicePlanStaffList")
    @ApiOperation("获取完成率列表")
    public Result practicePlanStaffList(@RequestParam Map reMap) {
        String yearPart = (String) reMap.get("yearPart");
        String quarterNum = (String) reMap.get("quarterNum");
        String subjectsId = (String) reMap.get("subjectsId");
        String startDate = (String) reMap.get("startDate");
        String endDate = (String) reMap.get("endDate");
        String type = (String) reMap.get("type");
        String practiceTypeId = (String) reMap.get("practiceTypeId");
        String lessonNumStart = (String) reMap.get("lessonNumStart");
        String lessonNumEnd = (String) reMap.get("lessonNumEnd");
        String lessonType = (String) reMap.get("lessonType");
        String schoolId = (String) reMap.get("schoolId");
        String yearClassId = (String) reMap.get("yearClassId");
        String classTypeId = (String) reMap.get("classTypeId");
        String teacherName = (String) reMap.get("teacherName");
        String classify = (String) reMap.get("classify");
        String trackType = (String) reMap.get("trackType");
        String teamId = (String) reMap.get("teamId");
        String trackId = (String) reMap.get("trackId");
        String gradeSection = (String) reMap.get("gradeSection");

        StringBuilder yearClassIdSb = new StringBuilder();
        if (gradeSection != null && !gradeSection.equals("")) {
            String[] sections = gradeSection.split(",");
            for (String section : sections) {
                switch (section) {
                    case "0":
                        yearClassIdSb.append("1,2");
                        break;
                    case "1":
                        yearClassIdSb.append("3,4,5,6");
                        break;
                    case "2":
                        yearClassIdSb.append("7,8,9");
                        break;
                    case "3":
                        yearClassIdSb.append("10,11,12");
                        break;
                }
                if (!section.equals(sections[sections.length - 1])) {
                    yearClassIdSb.append(",");
                }
            }
            yearClassId = yearClassIdSb.toString();
        }

        if (StringUtils.isBlank(schoolId)) {
            if (type.equals("2")) {
                //校区老师状态，只能看自己所在校区的数据
                schoolId = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
            }
        }

        List<PracticePlanStaff> practiceSubjectsTypeList = iPracticeSubjectsTypeService.practicePlanStaffList(type, practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, schoolId, yearClassId, classTypeId, teacherName, classify, trackType, teamId, trackId);

        return new Result(Code.OK, new PageResult<>(practiceSubjectsTypeList, (long) practiceSubjectsTypeList.size(), 1L));
    }

    @GetMapping("/practicePlanStaffDetails")
    @ApiOperation("获取完成率详情")
    public Result practicePlanStaffDetails(@RequestParam Map reMap) {
        String yearPart = (String) reMap.get("yearPart");
        String quarterNum = (String) reMap.get("quarterNum");
        String subjectsId = (String) reMap.get("subjectsId");
        String startDate = (String) reMap.get("startDate");
        String endDate = (String) reMap.get("endDate");
        String type = (String) reMap.get("type");
        String detailType = (String) reMap.get("detailType");// 1应完成 2已完成 3未完成 4完成
        String practiceTypeId = (String) reMap.get("practiceTypeId");
        String lessonNumStart = (String) reMap.get("lessonNumStart");
        String lessonNumEnd = (String) reMap.get("lessonNumEnd");
        String lessonType = (String) reMap.get("lessonType");
        String schoolId = (String) reMap.get("schoolId");
        String yearClassId = (String) reMap.get("yearClassId");
        String classTypeId = (String) reMap.get("classTypeId");
        String teacherId = (String) reMap.get("teacherId");
        String classify = (String) reMap.get("classify");
        String trackType = (String) reMap.get("trackType");
        String teamId = (String) reMap.get("teamId");
        String gradeSection = (String) reMap.get("gradeSection");

        StringBuilder yearClassIdSb = new StringBuilder();
        if (gradeSection != null && !gradeSection.equals("")) {
            String[] sections = gradeSection.split(",");
            for (String section : sections) {
                switch (section) {
                    case "0":
                        yearClassIdSb.append("1,2");
                        break;
                    case "1":
                        yearClassIdSb.append("3,4,5,6");
                        break;
                    case "2":
                        yearClassIdSb.append("7,8,9");
                        break;
                    case "3":
                        yearClassIdSb.append("10,11,12");
                        break;
                }
                if (!section.equals(sections[sections.length - 1])) {
                    yearClassIdSb.append(",");
                }
            }
            yearClassId = yearClassIdSb.toString();
        }

        List<PracticePlanStaff> practiceSubjectsTypeList = iPracticeSubjectsTypeService.practicePlanStaffDetails(type, practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, schoolId, yearClassId, classTypeId, teacherId, detailType, classify, trackType, teamId);

        return new Result(Code.OK, new PageResult<>(practiceSubjectsTypeList, (long) practiceSubjectsTypeList.size(), 1L));
    }

    @GetMapping("/getIntegralList")
    public Result getIntegralList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "-1") Integer pageSize,
                                  @RequestParam(value = "yearPart", required = false) String yearPart,
                                  @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                  @RequestParam(value = "subjectsId", required = false) String subjectsId,
                                  @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                  @RequestParam(value = "lessonType", required = false, defaultValue = "1") String lessonType,
                                  @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                  @RequestParam(value = "teacherName", required = false) String teacherName,
                                  @RequestParam(value = "lessonNumStart", required = false) String lessonNumStart,
                                  @RequestParam(value = "lessonNumEnd", required = false) String lessonNumEnd,
                                  @RequestParam(value = "startDate", required = false) String startDate,
                                  @RequestParam(value = "endDate", required = false) String endDate,
                                  @RequestParam(value = "schoolId", required = false) String schoolId,
                                  @RequestParam(value = "practiceTypeId", required = false) String practiceTypeId) {
        Quarter quarterNow = iQuarterService.getNowQuarter();
        if (StringUtils.isEmpty(yearPart)) {
            yearPart = quarterNow.getYearPart().toString();
            quarterNum = quarterNow.getNum().toString();
        }

        String type = "1";
        List<PracticePlanStaff> practicePlanList = iPracticeSubjectsTypeService.practicePlanStaffAllList(type, practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, schoolId, yearClassId, classTypeId, teacherName);
        return new Result(Code.OK, new PageResult<>(practicePlanList, (long) practicePlanList.size(), 1L));
    }

    @GetMapping("/getIntegralDetail")
    public Result getIntegralDetail(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    @RequestParam(value = "yearPart", required = false) String yearPart,
                                    @RequestParam(value = "quarterNum", required = false) String quarterNum,
                                    @RequestParam(value = "subjectsId", required = false) String subjectsId,
                                    @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                    @RequestParam(value = "lessonType", required = false, defaultValue = "1") String lessonType,
                                    @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                    @RequestParam(value = "teacherId", required = false) String teacherId,
                                    @RequestParam(value = "lessonNumStart", required = false) String lessonNumStart,
                                    @RequestParam(value = "lessonNumEnd", required = false) String lessonNumEnd,
                                    @RequestParam(value = "startDate", required = false) String startDate,
                                    @RequestParam(value = "endDate", required = false) String endDate,
                                    @RequestParam(value = "schoolId", required = false) String schoolId,
                                    @RequestParam(value = "practiceTypeId", required = false) String practiceTypeId) {
        Quarter quarterNow = iQuarterService.getNowQuarter();
        if (StringUtils.isEmpty(yearPart)) {
            yearPart = quarterNow.getYearPart().toString();
            quarterNum = quarterNow.getNum().toString();
        }

        Page<PracticePlanStaff> iPage = new Page<>(pageNum, pageSize);

        String type = "1";
        iPracticeSubjectsTypeService.practicePlanStaffAllDetail(iPage, type, practiceTypeId, yearPart, quarterNum, subjectsId, startDate, endDate, lessonNumStart, lessonNumEnd, lessonType, schoolId, yearClassId, classTypeId, teacherId);
        return new Result(Code.OK, new PageResult<>(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }

}
